<template>
<view>
<!--<import src="/templates/login/index.wxml"></import>-->

<view>
	<view class="detial">
		<view class="detial-title">{{content.title}}</view>
		<view class="detail-main">
			<view>{{formPlaceHolder.name}}：{{content.name}}</view>
			<view>{{formPlaceHolder.company}}：{{content.company}}</view>
			<view class="phone" v-if="statusPhone == false">
				<view>{{formPlaceHolder.phone}}：{{ noPhone}}</view>
				<view style="margin-right: 10rpx; background: #19a918; color: #fff;" @tap="getPhone">
					查看电话
				</view>
			</view>
			<view v-if="statusPhone == true">{{formPlaceHolder.phone}}：{{content.phone}}</view>
			<view>{{formPlaceHolder.number}}：{{content.number}}</view>
			<view>{{formPlaceHolder.claim}}：{{content.claim}}</view>
		</view>
		<view class="detail-foot">最后修改于：{{content.gmtModified}}</view>
	</view>
	<view class="foot">
		<!-- <view>点赞</view>
    <view>关注</view> -->
		<button :plain="status" @tap="collectFun">{{status ? '已收藏' : "收藏"}}</button>
		<button open-type="share" type="primary" plain="true">分享
		</button>
	</view>
</view>
<view v-if="!apiUserInfoMap" class="userinfo">
	<block data-type="template" data-is="apifmLogin" data-attr="ttlogin: ttlogin">
	<view class="apifmLogin" :hidden="ttlogin">
		<view class="s-b">
			<view class="s-l-b">
				<image src="/static/images/nologin.png"></image>
				<text>授权登录</text>
			</view>
			<view class="s-t-b">
				<view class="s-t-i">
					<text>·</text>
					请授权小程序登录
				</view>
				<view class="s-t-i">
					<text>·</text>
					我们不会公布您的这些信息
				</view>
				<view class="s-t-i">
					<text>·</text>
					只是为了给您提供更好的服务
				</view>
			</view>
			<button class="l" open-type="getUserInfo" @getuserinfo="processLogin">允许
			</button>
			<button class="c" @tap="cancelLogin" type="default">暂不登录
			</button>
		</view>
	</view>
</block>
</view>
</view>
</template>

<script>
// pages/common/category.js
const app = getApp();

export default {
  data() {
    return {
      ttlogin: true,
      content: {},
      userInfo: {},
      collect: {},
      status: false,
      integral: 0,
      a: {
        "message": "查看手机号-",
        "user": "1",
        "work": ""
      },
      b: {
        "category": "0",
        "origin": "查看手机号",
        "user": "1",
        "value": 1
      },
      statusPhone: false,
      formPlaceHolder: {
        title: "名称",
        claim: "招工要求",
        typeOfWork: "0",
        number: "招工人数",
        name: "联系人",
        company: "公司",
        phone: "手机号"
      },
      noPhone: '',
      apiUserInfoMap: ""
    };
  },

  components: {},
  props: {},
  onPullDownRefresh: function () {
    let id = this.content.id;
    uni.request({
      url: `${app.globalData.baseUrl}/release-work/work/one/${id}`,
      // 目标服务器url
      success: res => {
        let content = res.data.dataMap.data;

        if (content) {
          this.onGetUserInfo(id);
          this.setData({
            content
          });
        } else {
          uni.showModal({
            complete: res => {},
            content: '没有查到数据，可能已经删除',
            fail: res => {},
            showCancel: false,
            success: result => {
              uni.navigateBack();
            },
            title: '数据不存在'
          });
        }
      },
      complete: res => {
        uni.stopPullDownRefresh();
      }
    });
  },
  onLoad: function (options) {
    let id = options.id;
    uni.request({
      url: `${app.globalData.baseUrl}/release-work/work/one/${id}`,
      // 目标服务器url
      success: res => {
        let content = res.data.dataMap.data;

        if (content) {
          this.onGetUserInfo(id);
          this.setData({
            content,
            noPhone: content.phone.replace(/(\d{0,3})\d{0,4}(\d{0,})/, '$1****$2')
          });

          if (content.typeOfWork == 0) {
            this.setData({
              formPlaceHolder: {
                title: "工作意向",
                claim: "个人介绍",
                typeOfWork: "0",
                number: "年龄",
                name: "姓名",
                company: "技能",
                phone: "手机号"
              }
            });
            uni.setNavigationBarTitle({
              title: '名片详情'
            });
          }
        } else {
          uni.showModal({
            complete: res => {},
            content: '没有查到数据，可能已经删除',
            fail: res => {},
            showCancel: false,
            success: result => {
              uni.navigateBack();
            },
            title: '数据不存在'
          });
        }
      }
    });
    uni.showShareMenu({
      withShareTicket: true
    });
  },
  onShow: function () {
    uni.showShareMenu({
      withShareTicket: true
    });
  },
  onShareAppMessage: function (res) {
    let _this = this;

    if (res.from === 'button') {// 来自页面内转发按钮
    }

    return {
      title: `${_this.content.title}`,
      path: `/pages/common/detail/detail?id=${_this.content.id}`
    };
  },
  methods: {
    getPhone() {
      let a = this.a;
      let b = this.b;
      let user = this.userInfo.id;
      let work = this.content.id; //

      var userInfoParam = uni.getStorageSync('userInfo');
      console.log('userInfoParam', userInfoParam);

      if (userInfoParam) {
        this.setData({
          ttlogin: true,
          apiUserInfoMap: userInfoParam
        });
      } else {
        this.setData({
          ttlogin: false
        });
        uni.showToast({
          title: '授权后请下拉刷新当前页面',
          icon: "none"
        });
        return 0;
      } //


      if (this.integral == 0) {
        uni.showModal({
          cancelText: '返回列表',
          complete: res => {},
          confirmText: '获取积分',
          content: '您当前的积分不足，只需一个积分即可查看完整电话，是否前往获取积分？',
          fail: res => {},
          showCancel: true,
          success: result => {
            if (result.cancel) {
              uni.navigateBack();
            }

            if (result.confirm) {
              uni.navigateTo({
                url: '/pages/common/share/share'
              });
            }
          },
          title: '温馨提示'
        });
      } else {
        a.user = user;
        a.message = "查看手机号-" + this.content.title;
        a.work = work;
        b.user = user;
        uni.showLoading({
          title: '获取手机号'
        });
        uni.request({
          url: `${app.globalData.baseUrl}/bill`,
          complete: res => {
            uni.hideLoading();
            uni.startPullDownRefresh();
          },
          data: {
            'user': user,
            'work': work
          },
          fail: res => {},
          method: "POST",
          success: result => {
            uni.request({
              url: `${app.globalData.baseUrl}/integral`,
              complete: res => {},
              data: b,
              fail: res => {
                uni.showToast({
                  title: '网络错误',
                  icon: "none",
                  duration: 4000
                });
              },
              method: "POST",
              success: result => {
                uni.request({
                  url: `${app.globalData.baseUrl}/message/save`,
                  data: a,
                  method: "POST"
                });
              }
            });
          }
        });
      }
    },

    collectFun(e) {
      let user = this.userInfo.id;
      let work = this.content.id;
      let typeOfWork = this.content.typeOfWork;

      if (user == null) {
        uni.showToast({
          title: '登录才可以收藏',
          icon: "none"
        });
      } else {
        if (this.status) {
          this.remove();
          this.setData({
            status: false
          });
        } else {
          this.add(user, work, typeOfWork);
          this.setData({
            status: true
          });
        }
      }
    },

    getIntegral(user) {
      uni.request({
        url: `${app.globalData.baseUrl}/integral/sum/${user}`,
        complete: res => {},
        fail: res => {},
        success: result => {
          this.setData({
            integral: result.data.dataMap.data
          });
        }
      });
    },

    getPhoneStatus(user, work) {
      let _this = this;

      uni.request({
        url: `${app.globalData.baseUrl}/user-release-work/one/${work}`,

        success(res) {
          if (res.data.dataMap.data != null) {
            if (res.data.dataMap.data.userId != user) {
              uni.request({
                url: `${app.globalData.baseUrl}/bill/one/${user}/${work}`,
                complete: res => {},
                fail: res => {},
                success: result => {
                  let bill = result.data.dataMap.data;

                  if (bill != null) {
                    _this.setData({
                      statusPhone: true
                    });
                  }
                }
              });
            } else {
              _this.setData({
                statusPhone: true
              });
            }
          } else {
            uni.request({
              url: `${app.globalData.baseUrl}/bill/one/${user}/${work}`,
              complete: res => {},
              fail: res => {},
              success: result => {
                let bill = result.data.dataMap.data;

                if (bill != null) {
                  _this.setData({
                    statusPhone: true
                  });
                }
              }
            });
          }
        }

      });
    },

    getSatus: function (user, work) {
      let _this = this;

      uni.showLoading({
        title: '加载中'
      });
      uni.request({
        url: `${app.globalData.baseUrl}/collect/${user}/${work}`,
        complete: res => {
          uni.hideLoading();
        },
        fail: res => {},
        success: result => {
          if (result.data.dataMap.data == null || result.data.dataMap.data == undefined) {
            _this.setData({
              status: false
            });
          } else {
            _this.setData({
              status: true,
              collect: result.data.dataMap.data
            });
          }
        }
      });
    },

    onGetUserInfo(id) {
      let openid = uni.getStorageSync('openid');

      let _this = this;

      if (!openid) {
        uni.showToast({
          title: '未登录',
          icon: "none"
        });
      } else {
        uni.request({
          url: `${app.globalData.baseUrl}/user/selectIsUser/${openid}`,
          complete: res => {},
          success: result => {
            _this.setData({
              userInfo: result.data.dataMap.data
            });

            let userInfo = _this.userInfo;

            _this.getSatus(userInfo.id, id);

            _this.getIntegral(userInfo.id);

            _this.getPhoneStatus(userInfo.id, _this.content.id);
          }
        });
      }
    },

    add(user, work, typeOfWork) {
      uni.showLoading({
        title: '收藏中'
      });
      let from = {
        typeOfWork,
        user,
        work
      };
      uni.request({
        url: `${app.globalData.baseUrl}/collect/save`,
        complete: res => {
          uni.hideLoading();
        },
        data: from,
        fail: res => {},
        method: "POST",
        success: result => {}
      });
    },

    remove() {
      let _this = this;

      uni.showLoading({
        title: '取消收藏'
      });
      uni.request({
        url: `${app.globalData.baseUrl}/collect?id=${_this.collect.id}`,
        complete: res => {
          uni.hideLoading();
        },
        fail: res => {},
        method: "DELETE",
        success: result => {}
      });
    },

    onShareTimeline: function (res) {},
    //授权登录绑定按钮
    processLogin: function (e) {
      app.globalData.globalHandleLogin(e);
      this.setData({
        apiUserInfoMap: app.globalData.userInfoData
      });
      this.onLoad();
    }
  }
};
</script>
<style>
@import "../../../templates/login/index.css";

page {
    background: #efeff5;
}

.detial-title {
    width: 96%;
    margin: 2%;
    height: 100rpx;
    line-height: 100rpx;
    font-size: large;
    font-weight: 1000;
    border-bottom: 1px solid #e6e6e6;
    padding-left: 10rpx;
    background: #fff;
}

.detail-main {
    width: 96%;
    margin: 2%;
    line-height: 70rpx;
    padding-left: 10rpx;
    margin-top: 10rpx;
    background: #fff;
}

.detail-foot {
    width: 96%;
    margin: 2%;
    line-height: 70rpx;
    padding-right: 20rpx;
    margin-top: 10rpx;
    text-align: right;
}

.foot {
    position: absolute;
    bottom: 10rpx;
    width: 96%;
    margin: 2%;
    display: flex;
    text-align: center;
}
.foot button {
    margin: 0rpx 10rpx;
    width: 96%;
    background: #fff;
}

.foot view {
    flex: 1;
    height: 100rpx;
    line-height: 100rpx;
}

.phone {
    display: flex;
    justify-content: space-between;
}
</style>